package com.ctsi.sms;

import com.ctsi.sms.codec.smgp.msg.SMGPSubmitMessage;
import com.ctsi.sms.common.util.ChannelUtil;
import com.ctsi.sms.connect.manager.EndpointConnector;
import com.ctsi.sms.connect.manager.EndpointEntity;
import com.ctsi.sms.connect.manager.EndpointManager;
import com.ctsi.sms.connect.manager.smgp.SMGPClientEndpointEntity;
import io.netty.channel.ChannelFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;

@EnableScheduling
@SpringBootApplication
public class SmsApplication {

    private static final Logger logger = LoggerFactory.getLogger(SmsApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SmsApplication.class, args);
    }

    @PostConstruct
    public void initSMS() throws Exception {
        final EndpointManager manager = EndpointManager.INS;

        SMGPClientEndpointEntity client = new SMGPClientEndpointEntity();
        client.setId("zzzx-sms-client");
        client.setHost("219.146.6.68");
        client.setPort(3058);
        client.setClientID("zhzhzx");
        client.setPassword("zhzhzx20");
        client.setChannelType(EndpointEntity.ChannelType.DUPLEX);

        client.setMaxChannels((short) 1);
        client.setRetryWaitTimeSec((short) 100);
        client.setUseSSL(false);
        client.setReSendFailMsg(false);
        client.setClientVersion((byte) 0x30);
        client.setWriteLimit(200);
        client.setReadLimit(200);
        manager.addEndpointEntity(client);
        manager.startConnectionCheckTask();
        manager.openAll();
        logger.info("SMS service startUp.............");
        logger.info("begin send sms for test.............");
    }

    @Scheduled(cron = "0 */20 * * * ?")
    public void sendSms() {
        logger.warn("短信发送测试开始!");
        EndpointEntity en = EndpointManager.INS.getEndpointEntity("zzzx-sms-client");
        EndpointConnector conn = EndpointManager.INS.getEndpointConnector("zzzx-sms-client");
        String mes = "最大连接数:" + en.getMaxChannels() + "当前连接数：" + conn.getConnectionNum();
        BaseMessage msg = createMessage(mes, "18953132550");
        try {
            ChannelFuture promises = ChannelUtil.asyncWriteToEntity("zzzx-sms-client", msg, future -> {
                if (future.isSuccess()) {
                    logger.warn("sms send to {} success!", "18953132550");
                } else {
                    logger.warn("sms send to {} faild!", "18953132550");
                    String err = future.cause().getMessage();
                    logger.error("send sms error: {}");
                }
            });
        } catch (Exception e) {
            logger.error("send sms occur error: {}", e);
        }
    }


    public static BaseMessage createMessage(String msgContent, String phone) {
        SMGPSubmitMessage pdu = new SMGPSubmitMessage();
        pdu.setSrcTermId("106597881");
        pdu.setDestTermIdArray(phone);
        pdu.setMsgContent(msgContent);
        pdu.setServiceId("20001");
        pdu.setNeedReport(true);
        pdu.setFeeType("00");
        pdu.setFeeCode("0");
        return pdu;
    }


}
